<template>
  <el-card class="notification-group-card" shadow="hover" @click="handleClick">
    <div class="card-content">
      <!-- 头像组 -->
      <div class="avatars-group">
        <el-tooltip
          v-for="(sender, index) in displaySenders"
          :key="sender.userId"
          :content="getSenderTooltip(sender)"
          placement="top"
        >
          <el-avatar
            :src="getImageUrl(sender.avatar)"
            :size="48"
            class="sender-avatar"
            :style="{ zIndex: displaySenders.length - index }"
          >
            <IconifyIconOffline icon="ep:user" />
          </el-avatar>
        </el-tooltip>
        <div v-if="notification.otherSendersCount > 0" class="more-count">
          +{{ notification.otherSendersCount }}
        </div>
      </div>

      <!-- 通知信息 -->
      <div class="notification-info">
        <!-- 未读标记 -->
        <el-badge
          v-if="notification.unreadCount > 0"
          :value="notification.unreadCount"
          :max="99"
          class="unread-badge"
        />

        <!-- 聚合标题 -->
        <div class="notification-title">
          {{ notification.aggregatedTitle }}
        </div>

        <!-- 底部信息 -->
        <div class="notification-footer">
          <div class="footer-left">
            <el-tag :type="getTypeTagStyle(notification.type)" size="small">
              {{ notification.typeName }}
            </el-tag>
            <span class="time-text">
              <IconifyIconOffline icon="ep:clock" />
              {{ formatRelativeTime(notification.latestTime) }}
            </span>
          </div>

          <div class="footer-actions" @click.stop>
            <el-button
              v-if="!notification.allRead"
              link
              type="primary"
              size="small"
              @click="handleMarkRead"
            >
              <IconifyIconOffline icon="ep:check" />
              标记已读
            </el-button>
            <span v-else class="read-text">已读</span>
            <el-button
              link
              type="primary"
              size="small"
              @click="handleViewDetail"
            >
              <IconifyIconOffline icon="ep:view" />
              查看详情
            </el-button>
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { computed } from "vue";
import { IconifyIconOffline } from "@/components/ReIcon";
import type { GroupedNotification } from "@/types/notification-grouped";
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import "dayjs/locale/zh-cn";

dayjs.extend(relativeTime);
dayjs.locale("zh-cn");

interface Props {
  notification: GroupedNotification;
}

interface Emits {
  (e: "click", notification: GroupedNotification): void;
  (e: "markRead", notification: GroupedNotification): void;
  (e: "viewDetail", notification: GroupedNotification): void;
}

const props = defineProps<Props>();
const emit = defineEmits<Emits>();

// 最多显示3个头像
const displaySenders = computed(() => {
  return (props.notification.senders || []).slice(0, 3);
});

// 获取发送者提示信息
const getSenderTooltip = (sender: any) => {
  let tooltip = sender.nickName;
  if (sender.latestTime) {
    tooltip += ` - ${formatRelativeTime(sender.latestTime)}`;
  }
  return tooltip;
};

// 处理图片URL
const getImageUrl = (url: string) => {
  if (!url) return "";
  if (url.startsWith("http")) return url;
  return `http://localhost:8081${url.startsWith("/") ? "" : "/"}${url}`;
};

// 获取类型标签样式
const getTypeTagStyle = (type: string) => {
  const styleMap: Record<string, any> = {
    FOLLOW: "success",
    LIKE: "danger",
    COLLECT: "warning",
    COMMENT: "primary",
    REPLY: "primary",
    MENTION: "info",
    SYSTEM: ""
  };
  return styleMap[type] || "";
};

// 格式化相对时间
const formatRelativeTime = (time: string) => {
  return dayjs(time).fromNow();
};

// 处理点击
const handleClick = () => {
  emit("click", props.notification);
};

// 处理标记已读
const handleMarkRead = () => {
  emit("markRead", props.notification);
};

// 处理查看详情
const handleViewDetail = () => {
  emit("viewDetail", props.notification);
};
</script>

<style scoped lang="scss">
// 移动端适配
@media screen and (width <= 768px) {
  .notification-group-card {
    margin-bottom: 12px;

    :deep(.el-card__body) {
      padding: 12px;
    }

    .card-content {
      gap: 12px;

      .avatars-group {
        width: 48px;
        height: 40px;

        .sender-avatar {
          width: 40px;
          height: 40px;
        }

        .more-count {
          width: 24px;
          height: 24px;
          font-size: 11px;
        }
      }

      .notification-info {
        .notification-title {
          font-size: 14px;
        }

        .notification-content {
          font-size: 12px;
        }

        .notification-footer {
          flex-direction: column;
          gap: 8px;
          align-items: flex-start;

          .footer-actions {
            width: 100%;

            .el-button {
              flex: 1;
            }
          }
        }
      }
    }
  }
}

.notification-group-card {
  margin-bottom: 16px;
  cursor: pointer;
  transition: all 0.3s;

  &:hover {
    transform: translateY(-2px);
  }

  :deep(.el-card__body) {
    padding: 16px;
  }

  .card-content {
    display: flex;
    gap: 16px;

    .avatars-group {
      position: relative;
      display: flex;
      flex-shrink: 0;
      width: 60px;
      height: 48px;

      .sender-avatar {
        position: absolute;
        border: 2px solid #fff;
        box-shadow: 0 2px 4px rgb(0 0 0 / 10%);

        &:nth-child(1) {
          left: 0;
        }

        &:nth-child(2) {
          left: 12px;
        }

        &:nth-child(3) {
          left: 24px;
        }
      }

      .more-count {
        position: absolute;
        right: 0;
        bottom: 0;
        z-index: 10;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 28px;
        height: 28px;
        font-size: 12px;
        color: #fff;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border: 2px solid #fff;
        border-radius: 50%;
        box-shadow: 0 2px 4px rgb(0 0 0 / 10%);
      }
    }

    .notification-info {
      position: relative;
      flex: 1;
      min-width: 0;

      .unread-badge {
        position: absolute;
        top: -4px;
        right: -4px;
      }

      .notification-title {
        margin-bottom: 8px;
        overflow: hidden;
        text-overflow: ellipsis;
        font-size: 15px;
        font-weight: 500;
        line-height: 1.5;
        color: #303133;
        white-space: nowrap;
      }

      .notification-content {
        display: -webkit-box;
        margin-bottom: 8px;
        overflow: hidden;
        text-overflow: ellipsis;
        -webkit-line-clamp: 2;
        font-size: 13px;
        line-height: 1.6;
        color: #606266;
        -webkit-box-orient: vertical;
      }

      .notification-footer {
        display: flex;
        gap: 12px;
        align-items: center;
        justify-content: space-between;

        .footer-left {
          display: flex;
          flex: 1;
          gap: 8px;
          align-items: center;
          min-width: 0;

          .time-text {
            display: flex;
            gap: 4px;
            align-items: center;
            margin-top: 2px;
            font-size: 12px;
            color: #909399;
          }
        }

        .footer-actions {
          display: flex;
          flex-shrink: 0;
          gap: 8px;
          align-items: center;
          min-height: 28px;

          .read-text {
            padding: 4px 12px;
            font-size: 12px;
            color: #909399;
          }

          .el-button {
            padding: 4px 8px;
            font-size: 12px;
          }
        }
      }
    }
  }
}
</style>
